package com.example.ccjml.ui.theme;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.ccj.b.a.BaseActivity;
import com.example.ccj.b.c.custom.MButton;
import com.example.ccj.b.c.custom.MEditTextView;
import com.example.ccj.b.c.custom.MTextView;
import com.example.ccj.b.c.draw.DrawFenShuView;
import com.example.ccj.b.c.draw.FenShu;
import com.example.ccj.b.c.draw.voice.CircleRoundView;
import com.example.ccj.b.c.draw.voice.OnTouchEventListener;
import com.example.ccj.u.GenerateId;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.NumberUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ThreadUtils;
import com.example.ccj.u.ThreadUtilsListener;
import com.example.ccjml.R;

import java.util.concurrent.TimeUnit;

public class TestFenShuActivity extends BaseActivity {

    MEditTextView fenzi;
    MEditTextView fenmu;
    DrawFenShuView drawFenShuView;
    ImageView img;

    @Override
    public void initData(@Nullable Bundle bundle) {

        fenzi.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (!StringUtils.isEmpty(s)) drawFenShuView.setResultFenZi(Integer.parseInt(s.toString()));
            }
        });
        fenmu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (!StringUtils.isEmpty(s)) drawFenShuView.setResultFenMu(Integer.parseInt(s.toString()));
            }
        });

        drawFenShuView.setFenShuList(new FenShu[]{new FenShu(58,89,true),
                new FenShu(58,89,false),
                new FenShu(58,89,false),
                new FenShu(58,89,true)},18,false);

        img = findViewById(R.id.activity_test_fen_shu_img);

        MButton mButton = findViewById(R.id.activity_test_fen_shu_save);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = drawFenShuView.getBitmap();
                if (bitmap != null) {
                    img.setImageBitmap(bitmap);
                } else {
                    LogUtils.e("输出图片为空！");
                }

//                drawFenShuView.saveSignatureToFile(GenerateId.getNoRepeatTimeIdToStr());
            }
        });

        MTextView mTextView = findViewById(R.id.activity_test_fen_ttttt);
        mTextView.setMText("前()()缀&///[{\"color\":-16777216,\"fenMu\":92,\"fenZi\":90,\"isJiaFa\":false}]///&后缀前缀&///[{\"color\":-16777216,\"fenMu\":92,\"fenZi\":90,\"isJiaFa\":false},{\"color\":-16777216,\"fenMu\":92,\"fenZi\":12,\"isJiaFa\":true}]///&后缀");


        MButton addText = findViewById(R.id.activity_test_fen_shu_add_text);
        addText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextView.setTianKongText("测试123",0);
                LogUtils.d(mTextView.getPureText());
            }
        });




        CircleRoundView circleRoundView = findViewById(R.id.activity_test_fen_circle);
        MButton startCircleRoundView = findViewById(R.id.activity_test_fen_shu_start_circle);
        circleRoundView.setOnTouchEvent3(new OnTouchEventListener() {
            @Override
            public void down() {
                threadUtilsListener = new ThreadUtilsListener<Integer>() {
                    @Override
                    public Integer doInBackground() throws Throwable {
                        return NumberUtils.getRangeNumber(410, 0);
//                       return 52;
                    }

                    @Override
                    public void onSuccess(Integer result) {
                        circleRoundView.setWaveData(null, result);
                    }
                };

                ThreadUtils.executeBySingleAtFixRate(threadUtilsListener,50, TimeUnit.MILLISECONDS);
            }

            @Override
            public void up() {
                ThreadUtils.cancel(threadUtilsListener);
            }
        });
    }
   ThreadUtilsListener<Integer> threadUtilsListener;
    @Override
    public void doBusiness() {

    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState, @Nullable View contentView) {
        drawFenShuView = findViewById(R.id.activity_main_test_navigation_12);
        fenzi = findViewById(R.id.activity_test_fen_shu_fenzi);
        fenmu = findViewById(R.id.activity_test_fen_shu_fenmu);
    }

    @Override
    public int initContentView() {
        return R.layout.activity_test_fen_shu;
    }




}